<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" 	uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"  %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" 	%> 

<jsp:include page="./dbChatConfig.jsp" flush="false" />

<sql:query var="dualTable" dataSource="${oracleDs}" >
	select SYSDATE from dual
</sql:query>

<c:if test="${ param.chatRoomId ne null and param.userName ne null  }" >
	<sql:transaction dataSource="${oracleDs}" > 
		<sql:update var="chatMsgInsCnt" >
			/* 채팅 메시지 입력 */
			INSERT INTO chat_msg
			( chat_msg_id, chat_msg_room_id, chat_msg_user_name , chat_msg_text )
			SELECT
			  CHAT_MSG_ID_SEQ.NEXTVAL , chat_room_id , ? , ?
			FROM chat_room
			WHERE 1 = 1
			AND chat_room_id = ?
			AND chat_room_valid = 1
			AND ? IS NOT NULL
			AND ? IS NOT NULL
			<sql:param value="${ param.userName	 	}"	/> 
			<sql:param value="${ param.msgText	 	}"	/> 
			<sql:param value="${ param.chatRoomId	}"	/>
			<sql:param value="${ param.userName	 	}"	/> 
			<sql:param value="${ param.msgText	 	}"	/>
			/* 끝. 채팅 메시지 입력 생성 */
		</sql:update> 
	</sql:transaction>
</c:if>

<sql:query var="chatRoom" dataSource="${oracleDs}" >
	/* 채팅방 상태 조회 */
	SELECT chat_room_id, chat_room_valid ,
		 chat_room_name, chat_room_user_name ,
	   1 AS a
	FROM chat_room
	WHERE 1 = 1
	AND chat_room_id = ?
	<sql:param value="${ param.chatRoomId }" />
	/* 끝. 채팅방 상태 조회 */
</sql:query> 

<sql:query var="chatMsgList" dataSource="${oracleDs}" >  
	/* 채팅 메시지 목록 조회 */
	SELECT
		ROWNUM AS chat_msg_no ,
		chat_room_id , chat_msg_id ,
		TO_CHAR( chat_msg_cre_date,'YYYY-MM-DD HH24:MI:SS') AS chat_msg_cre_date ,
		chat_msg_user_name ,
		chat_msg_text ,
		1 as a
	FROM chat_room , chat_msg
	WHERE 1 = 1
	AND chat_room.chat_room_id = chat_msg.chat_msg_room_id
	AND chat_room_id = ?
	AND chat_room_valid = 1
	AND chat_msg_valid = 1
	ORDER BY chat_room_id, chat_msg_id
	<sql:param value="${ param.chatRoomId }" />
    /* 끝. 채팅 메시지 목록 조회 */
</sql:query> 

<c:set var="a" value="채팅방 상태 확인" />

<c:set var="chatRoomValid" value="-1" />
<c:forEach items="${chatRoom.rows}" var="row" varStatus="status" >
	<c:set var="chatRoomValid" value="${ row.CHAT_ROOM_VALID }" />
</c:forEach>

<c:set var="bodyBackgroundColor" value="${ chatRoomValid == 1 ? 'white' : 'gray' }" />

<c:set var="refCnt" value="${ param.refCnt + 1 }" /> 
<jsp:useBean id="now" class="java.util.Date"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="max-age=0" />
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="expires" content="0" />

	<c:if test="${ chatRoomValid == 1 }" >
	<meta http-equiv="refresh" 		content="5;URL=?chatRoomId=${ param.chatRoomId }&refCnt=${ refCnt }#bottom" >
	</c:if>
	
	<title>JSP Chat Message List (DB)</title>
</head>
<body style="background-color: ${ bodyBackgroundColor };" >
	<div >
		<ul style="padding-left: 20px;" >
			<c:if test="${ chatRoomValid < 1 }" >
				<li>
					삭제된 게시판 입니다.
				</li>
			</c:if>
			<c:if test="${ chatRoomValid == 1 }" >
				<li>
					<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss" />
					:
					( ${ refCnt } )
				</li> 
			</c:if>
		</ul>
	</div>
	<div>
		<ol style="padding-left: 30px;" >
			<c:forEach items="${chatMsgList.rows}" var="row" varStatus="status" >
				<li>
					${ row.CHAT_MSG_CRE_DATE }
					<br/>
					${ row.CHAT_MSG_USER_NAME }
					 : 
					${ row.CHAT_MSG_TEXT }
				</li>
			</c:forEach> 			
		</ol>
	</div>
	<div id="bottom" ></div> 
</body>
</html>